//package com.auxgroup.smarthome.qcloud.controller;
//
//import com.alibaba.fastjson.JSONObject;
//import com.auxgroup.smarthome.web.ApiResponse;
//import com.auxgroup.smarthome.qcloud.bean.QcloudConf;
//import com.qcloud.cos.COSClient;
//import com.qcloud.cos.ClientConfig;
//import com.qcloud.cos.exception.AbstractCosException;
//import com.qcloud.cos.meta.InsertOnly;
//import com.qcloud.cos.request.*;
//import com.qcloud.cos.sign.Credentials;
//import com.qcloud.cos.sign.Sign;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiImplicitParam;
//import io.swagger.annotations.ApiImplicitParams;
//import io.swagger.annotations.ApiOperation;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.multipart.MultipartHttpServletRequest;
//import org.springframework.web.multipart.commons.CommonsMultipartResolver;
//import springfox.documentation.annotations.ApiIgnore;
//
//import javax.servlet.http.HttpServletRequest;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.Iterator;
//import java.util.List;
//
///**
// * Created by gizwits on 2017/4/25.
// */
//@Api(value = "腾讯云储存相关操作", tags = "腾讯云")
//@RestController
//@RequestMapping("/qcloud")
//public class QCloudController {
//    @Autowired
//    private QcloudConf qcloudConf;
//
//    @ApiOperation(value = "上传文件", notes = "单个上传文件，上传成功返回文件路径")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "uploadFile", value = "上传的文件", required = true, paramType = "form", dataType = "file"),
//    })
//    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
//    public ApiResponse<JSONObject> uploadFile(@RequestParam("uploadFile") MultipartFile file) throws IOException {
//        if (file == null) {
//            return ApiResponse.prompt("-100", "请选择文件");
//        }
//        COSClient cosClient = getCOSClient();
//        JSONObject jsonObject = handleUpload(cosClient, file);
//        // 关闭释放资源
//        cosClient.shutdown();
//        return ApiResponse.ok(jsonObject);
//    }
//
//    @ApiOperation(value = "批量上传文件", notes = "批量上传文件，上传成功返回文件路径, 上传文件名字命名任意，可以上传N个文件，最多同时上传6个")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "uploadFile", value = "上传的文件", paramType = "form", dataType = "file"),
//            @ApiImplicitParam(name = "uploadFile1", value = "上传的文件", paramType = "form", dataType = "file"),
//            @ApiImplicitParam(name = "uploadFile2", value = "上传的文件", paramType = "form", dataType = "file"),
//    })
//    @RequestMapping(value = "/uploadFile/multi", method = RequestMethod.POST)
//    public ApiResponse<List<JSONObject>> uploadMultiFile(@ApiIgnore HttpServletRequest request) throws IOException {
//
//        // 创建一个通用的多部分解析器
//        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//        //判断 request 是否有文件上传,即多部分请求
//        List<JSONObject> ret = new ArrayList<>(); // 返回结果集
//        if (multipartResolver.isMultipart(request)) {
//            //转换成多部分request
//            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
//            //取得request中的所有文件名
//            Iterator<String> iterator = multiRequest.getFileNames();
//            COSClient cosClient = getCOSClient();
//            ///////////////////////////////////////////////////////////////
//            // 文件操作 //
//            ///////////////////////////////////////////////////////////////
//            while (iterator.hasNext()) {
//                MultipartFile multifile = multiRequest.getFile(iterator.next());
//                JSONObject jsonObject = handleUpload(cosClient, multifile);
//                ret.add(jsonObject);
//            }
//        }
//
//        return ApiResponse.ok(ret);
//    }
//
//    private JSONObject handleUpload(COSClient cosClient, MultipartFile file) throws IOException {
//        // 上传文件(默认不覆盖)
//        UploadFileRequest uploadFileRequest = new UploadFileRequest(qcloudConf.getBucketName(), qcloudConf.getCosPath() + file.getOriginalFilename(), file.getBytes());
//        uploadFileRequest.setInsertOnly(InsertOnly.NO_OVER_WRITE); // 设置成不覆盖
//        uploadFileRequest.setEnableShaDigest(false);
//        String uploadFileRet = cosClient.uploadFile(uploadFileRequest);
//        JSONObject jsonObject = JSONObject.parseObject(uploadFileRet);
//        return jsonObject;
//    }
//
//    @ApiOperation(value = "下载文件", notes = "")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "cosPath", value = "下载的路径", required = true, paramType = "form", dataType = "string"),
//    })
//    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
//    public JSONObject downloadFile(String cosPath) {
//        COSClient cosClient = getCOSClient();
//        String localPathDown = "src/test/resources/local_file_down.txt";
//        GetFileLocalRequest getFileLocalRequest =
//                new GetFileLocalRequest(qcloudConf.getBucketName(), qcloudConf.getCosPath() + cosPath, localPathDown);
//        getFileLocalRequest.setUseCDN(false);
//        getFileLocalRequest.setReferer("*.myweb.cn");
//        String getFileResult = cosClient.getFileLocal(getFileLocalRequest);
//
//        System.out.println("getFileResult:" + getFileResult);
//        // 关闭释放资源
//        cosClient.shutdown();
//        JSONObject jsonObject = JSONObject.parseObject(getFileResult);
//        return jsonObject;
//    }
//
//    @ApiOperation(value = "移动文件", notes = "", hidden = true)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "cosFilePath", value = "原路径", required = true, paramType = "form", dataType = "string"),
//            @ApiImplicitParam(name = "dstFilePath", value = "目标路径", required = true, paramType = "form", dataType = "string"),
//    })
//    @RequestMapping(value = "/moveFile", method = RequestMethod.POST)
//    public JSONObject moveFile(@ApiIgnore String cosFilePath, @ApiIgnore String dstFilePath) {
//        COSClient cosClient = getCOSClient();
//        MoveFileRequest moveRequest = new MoveFileRequest(qcloudConf.getBucketName(), cosFilePath, dstFilePath);
//        String moveFileRet = cosClient.moveFile(moveRequest);
//        System.out.println("move file ret:" + moveFileRet);
//        // 关闭释放资源
//        cosClient.shutdown();
//        JSONObject jsonObject = JSONObject.parseObject(moveFileRet);
//        return jsonObject;
//    }
//
//    @ApiOperation(value = "删除文件", notes = "", hidden = true)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "cosFilePath", value = "cos路径", required = true, paramType = "form", dataType = "string"),
//    })
//    @RequestMapping(value = "/deleteFile", method = RequestMethod.DELETE)
//    public JSONObject deleteFile(String cosFilePath) {
//        COSClient cosClient = getCOSClient();
//        DelFileRequest delFileRequest = new DelFileRequest(qcloudConf.getBucketName(), cosFilePath);
//        String delFileRet = cosClient.delFile(delFileRequest);
//        System.out.println("del file ret:" + delFileRet);
//        // 关闭释放资源
//        cosClient.shutdown();
//        JSONObject jsonObject = JSONObject.parseObject(delFileRet);
//        return jsonObject;
//    }
//
//    @ApiOperation(value = "创建目录", notes = "", hidden = true)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "cosPath", value = "cos路径", required = true, paramType = "form", dataType = "string"),
//    })
//    @RequestMapping(value = "/createFolder", method = RequestMethod.POST)
//    public JSONObject createFolder(String cosPath) {
//        COSClient cosClient = getCOSClient();
//        CreateFolderRequest createFolderRequest = new CreateFolderRequest(qcloudConf.getBucketName(), cosPath);
//        String createFolderRet = cosClient.createFolder(createFolderRequest);
//        // 关闭释放资源
//        cosClient.shutdown();
//        JSONObject jsonObject = JSONObject.parseObject(createFolderRet);
//        return jsonObject;
//    }
//
//    @ApiOperation(value = "获取多次签名", notes = "获取多次签名, 一段时间内有效, 针对上传文件，重命名文件, 创建目录, 获取文件目录属性, 拉取目录列表")
//    @RequestMapping(value = "/getPeriodEffectiveSign", method = RequestMethod.GET)
//    public ApiResponse<String> getPeriodEffectiveSign() throws AbstractCosException {
//        Credentials cred = new Credentials(qcloudConf.getAppId(), qcloudConf.getSecretId(), qcloudConf.getSecretKey());
//        return ApiResponse.ok(Sign.getPeriodEffectiveSign(qcloudConf.getBucketName(), qcloudConf.getCosPath(), cred, System.currentTimeMillis() / 1000 + qcloudConf.getExpired()));
//    }
//
//    @ApiOperation(value = "获取单次签名", notes = "获取单次签名, 一次有效，针对删除和更新文件目录")
//    @RequestMapping(value = "/getOneEffectiveSign", method = RequestMethod.GET)
//    public ApiResponse<String> getOneEffectiveSign() throws AbstractCosException {
//        Credentials cred = new Credentials(qcloudConf.getAppId(), qcloudConf.getSecretId(), qcloudConf.getSecretKey());
//        return ApiResponse.ok(Sign.getOneEffectiveSign(qcloudConf.getBucketName(), qcloudConf.getCosPath(), cred));
//    }
//
//
//    @ApiOperation(value = "下载签名", notes = "下载签名, 用于获取后拼接成下载链接，下载私有bucket的文件")
//    @RequestMapping(value = "/getDownLoadSign", method = RequestMethod.GET)
//    public ApiResponse<String> getDownLoadSign() throws AbstractCosException {
//        Credentials cred = new Credentials(qcloudConf.getAppId(), qcloudConf.getSecretId(), qcloudConf.getSecretKey());
//        return ApiResponse.ok(Sign.getDownLoadSign(qcloudConf.getBucketName(), qcloudConf.getCosPath(), cred, System.currentTimeMillis() / 1000 + qcloudConf.getExpired()));
//    }
//
//    public COSClient getCOSClient() {
//        // 初始化秘钥信息
//        Credentials cred = new Credentials(qcloudConf.getAppId(), qcloudConf.getSecretId(), qcloudConf.getSecretKey());
//        // 初始化客户端配置
//        ClientConfig clientConfig = new ClientConfig();
//        // 设置bucket所在的区域，比如华南园区：gz； 华北园区：tj；华东园区：sh ；
//        clientConfig.setRegion(qcloudConf.getRegion());
//        // 初始化cosClient
//        COSClient cosClient = new COSClient(clientConfig, cred);
//        return cosClient;
//    }
//
//
//}
